package com.cmccgz.udacs_landlord_webapp.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.cmccgz.baselibrary.utils.StringUtils;
import com.cmccgz.udacs_landlord_webapp.R;
import com.cmccgz.udacs_landlord_webapp.bean.DeviceInfoBean;
import com.cmccgz.udacs_landlord_webapp.interfaces.OnRvItemClickListenter;

import java.util.List;

public class DevicesAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private final LayoutInflater inflater;
    private boolean openAllSelect;
    private List<DeviceInfoBean> datas;
    private Context mContext;
    private OnRvItemClickListenter<DeviceInfoBean> listenter;

    public DevicesAdapter(List<DeviceInfoBean> datas, Context mContext,boolean openAllSelect, OnRvItemClickListenter<DeviceInfoBean> listenter) {
        this.datas = datas;
        this.mContext = mContext;
        this.openAllSelect = openAllSelect;
        this.listenter = listenter;
        inflater = LayoutInflater.from(mContext);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case -1:
                return new EmptyViewHolder(inflater.inflate(R.layout.item_empty, parent, false));
            case 0:
                return new AllSelViewHolder(inflater.inflate(R.layout.item_device_sel_all, parent, false));
            case 1:
                return new NormalViewHolder(inflater.inflate(R.layout.item_device_sel, parent, false));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        switch (getItemViewType(position)) {
            case -1:
                ((EmptyViewHolder) holder).tv_empty.setText("暂无设备，下拉可刷新！");
                break;
            case 0:
                bindAllSelHolder((AllSelViewHolder) holder, position);
                break;
            case 1:
                bindNormalHolder((NormalViewHolder) holder, position);
                break;
        }
    }

    private void bindAllSelHolder(AllSelViewHolder holder, final int position) {
        if (listenter!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listenter.onItemClick(null,position,v);
                }
            });
        }
    }

    public void bindNormalHolder(NormalViewHolder holder, final int position) {
        NormalViewHolder normalViewHolder = holder;
        final DeviceInfoBean item = datas.get(position-(openAllSelect?1:0));
        if (item!=null) {
            normalViewHolder.tv_house_name.setText(StringUtils.isEmpty(item.house_name)?"未知房屋":item.house_name);
            normalViewHolder.tv_d_name.setText(StringUtils.isEmpty(item.device_name)?"未知设备":item.device_name);
        }
        if (listenter!=null) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listenter.onItemClick(item,position,v);
                }
            });
        }
    }
    @Override
    public int getItemCount() {
        return (datas == null || datas.size() == 0 ? 1 : datas.size()
                +(openAllSelect?1:0));
    }


    private class NormalViewHolder extends RecyclerView.ViewHolder {

        private final TextView tv_house_name;
        private final TextView tv_d_name;

        public NormalViewHolder(View itemView) {
            super(itemView);
            tv_house_name = ((TextView) itemView.findViewById(R.id.tv_house_name));
            tv_d_name = ((TextView) itemView.findViewById(R.id.tv_d_name));
        }
    }

    private class EmptyViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_empty;

        public EmptyViewHolder(View itemView) {
            super(itemView);
            tv_empty = ((TextView) itemView.findViewById(R.id.tv_empty));
        }
    }

    private class AllSelViewHolder extends RecyclerView.ViewHolder {
        private final TextView tv_empty;

        public AllSelViewHolder(View itemView) {
            super(itemView);
            tv_empty = ((TextView) itemView.findViewById(R.id.tv_empty));
        }
    }

    @Override
    public int getItemViewType(int position) {
        return datas == null || datas.size() == 0 ? -1 : (openAllSelect&&position==0?0:1);
    }


}
